/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.blitzcoder.collide.error;

import de.blitzcoder.collide.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

/**
 *
 * @author blitzcoder
 */
public class ApplicationError {

    private String threadName;
    private Date date;
    private Throwable error;
    
    public ApplicationError(Thread thread, Throwable exception) {
        this.threadName = thread.getName();
        this.error = exception;
        this.date = new Date(System.currentTimeMillis());
        print();
        
    }
    
    public void print() {
        
        print("---------------------------------------------------------");
        print(" APPLICATION ERROR");
        print("--------------------");
        print("Time: "+date.toString());
        print("Thread: "+threadName);
        print("Exception: ");
        error.printStackTrace();
        print("---------------------------------------------------------");
        
        
    }
    
    private void print(String str) {
        System.err.println(str);
    }
    
    public String getThreadName() {
        return threadName;
    }
    
    public Throwable getError() {
        return error;
    }
    
    public Date getDate() {
        return date;
    }
    
    public String generateError() {
        StringWriter sw = new StringWriter();
        getError().printStackTrace(new PrintWriter(sw));
        
        String out = "";
        
        out+="---------------------------------------------------------\n";
        out+=" APPLICATION ERROR\n";
        out+="--------------------\n";
        out+="Time: "+date.toString()+"\n";
        out+="Thread: "+threadName+"\n";
        out+="Exception: \n";
        out+=sw.toString()+"\n";
        out+="---------------------------------------------------------\n";
     
        return out;
    }
    
    public String generateEmailError() {
        
        StringWriter sw = new StringWriter();
        getError().printStackTrace(new PrintWriter(sw));
        
        String out = "";
        
        
        out+="\n\n\n";
        out+="Folgender Fehler ist aufgetreten:\n";
        out+="--------------------------------------\n";
        out+="Time: "+date.toString()+"\n";
        out+="Thread: "+threadName+"\n";
        out+="Exception: \n";
        out+=sw.toString()+"\n";
        out+="--------------------------------------\n";
        
        Log.log(out);
        
        return out;
    }
    
}
